package com.tencent.qplus.paddata.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public DateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd");
	public DateFormat dateHourFormat  = new SimpleDateFormat("yyyy-MM-dd hh");
	
	
	public boolean isDateStr(String src,DateFormat format){
		try {
			format.parse(src);
			return true;
		} catch (ParseException e) {
			return false;
		}
	}
	
	public String format(Date date){
		return format(date,defaultFormat);
	}
	
	public String format(Date date,DateFormat format){
		return format.format(date);
	}
	public boolean isDateStr(String src){
		return isDateStr(src,defaultFormat);
	}
	
	public String getDateStrByDays(int days){
		return getDateStrByDays(days,defaultFormat);
	}
	
	public String getDateStrByDays(int days,DateFormat dateFormat){
		Date date = new Date();
		return getDateStrByDaysFromDate(date,days,dateFormat);
	}
	
	public String getDateStrByDaysFromDate(String date,int days,DateFormat dateFormat){
		try {
			Date src = dateFormat.parse(date);
			return getDateStrByDaysFromDate(src,days,dateFormat);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public String getDateStrByDaysFromDate(String date,int days){
		return getDateStrByDaysFromDate(date,days,defaultFormat);
	}
	
	public String getDateStrByDaysFromDate(Date date,int days,DateFormat dateFormat){
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.DAY_OF_YEAR, days);
		return dateFormat.format(cl.getTime());
	}
	
	public Date getDateDByDays(int days){
		return getDateByDays(days);
	}
	
	
	public Date getDateByDays(int days){
		Date date = new Date();
		return getDateByDaysFromDate(date,days);
	}
	
	public Date getDateByDaysFromDateStr(String date,int days,DateFormat dateFormat){
		try {
			Date src = dateFormat.parse(date);
			return getDateByDaysFromDate(src,days);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Date getDateByDaysFromDate(Date date,int days){
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.DAY_OF_YEAR, days);
		return cl.getTime();
	}
	
	
	private String[] getDateSlince(Date start,Date end,int field,int eachGap,DateFormat dateFormat){
		Calendar cl1 = Calendar.getInstance();
		Calendar cl2 = Calendar.getInstance();
		cl1.setTime(start);
		cl2.setTime(end);
		int gap =  cl2.get(field) - cl1.get(field);
		if(gap<=0){
			return null;
		}
		int len = (gap+eachGap)/eachGap;
		String[] rs = new String[len];
		for(int i=0;i<len;i++){
			rs[i] = dateFormat.format(cl1.getTime());
			cl1.add(field, eachGap);
		}
		return rs;
	}
	
	public String[] getDateDaySlince(Date start,Date end,int gapDays,DateFormat dateFormat){
		return getDateSlince(start,end,Calendar.DAY_OF_YEAR,gapDays,dateFormat);
	}
	
	public String[] getDateHourSlince(Date start,Date end,int gapHours,DateFormat dateFormat){
		return getDateSlince(start,end,Calendar.HOUR_OF_DAY,gapHours,dateFormat);
	}
}
